package com.woniu.publisher;

import com.woniu.util.ConstantUtil;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CustomPublisher implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void confirm(CorrelationData correlationData, boolean flag, String cause) {
        if(!flag){
            System.out.println("补偿机制");
        }
    }

    public void send(String msg,Integer ttl) {
//        rabbitTemplate.setConfirmCallback(this);
//        rabbitTemplate.setReturnsCallback(this);
        rabbitTemplate.convertAndSend(
                ConstantUtil.RABBIT_CUSTOM_EXCHANGE,
                ConstantUtil.ROUTING_KEY_CHECK_ORDER_STATUS,
                msg,
                message -> {
                    message.getMessageProperties().setDelay(ttl);
                    return message;
                }
        );

    }

    @Override
    public void returnedMessage(ReturnedMessage returned)  {
        System.out.println("消息没有到达队列");
    }
}
